package org.youchan.aco.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MainMenu extends JMenuBar  {
	private static final long serialVersionUID = 3538507976434347716L;

	private enum Action {
		EXIT("file.exit"), SETTING("window.setting");
		
		private String name;
		private Action(String name) {
			this.name = name;
		}
		public String getName() {
			return this.name;
		}
	}
	
	public MainMenu() {
		super();
		init();
	}
	
	private void init() {
		Action[] actions = Action.values();
		for (Action action: actions) {
			String[] name = action.getName().split("\\.");
			JMenu menu = null;
			int count = this.getMenuCount();
			for (int i = 0; i < count; i ++) {
				if (this.getMenu(i).getName().equals(name[0])) {
					menu = this.getMenu(i);
				}
			}
			if (menu == null) {
				menu = new JMenu(UIManager.getResource("menu." + name[0]));
				menu.setName("menu." + name[0]);
				this.add(menu);
			}
			addMenuItem(menu, action);
		}
	}
	
	private void addMenuItem(JMenu menu, final Action action) {
		JMenuItem item = new JMenuItem(UIManager.getResource("menu." + action.getName()));
		item.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				doAction(action);
			}
		});
		menu.add(item);
	}

	public void doAction(Action action) {
		switch (action) {
		case EXIT:
			UIManager.getInstance().exit();
			break;
		case SETTING:
			UIManager.getInstance().showSettingWindow();
			break;
		}
	}
}
